﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using HandyControl.Controls;
using System.Windows.Threading;
using BtLabelBLL;

namespace 功能测试软件.View
{
    /// <summary>
    /// TestView.xaml 的交互逻辑
    /// </summary>
    public partial class TestView : UserControl
    {
        /// <summary>
        /// 配置文件model
        /// </summary>
        public static Model.ConfigModel config = new Model.ConfigModel();
        /// <summary>
        /// Mac列表锁
        /// </summary>
        public static readonly object MacsLock = new object();
        /// <summary>
        /// 蓝牙搜索的Mac列表
        /// </summary>
        public static List<BtLabelModel.DeviceModel> Macs = new List<BtLabelModel.DeviceModel>();
        /// <summary>
        /// 蓝牙模块搜索状态
        /// </summary>
        public static bool isScan = false;
        /// <summary>
        /// 保存数据方法类
        /// </summary>
        public static BtLabelDAL.TestDAL resultDAL = new BtLabelDAL.TestDAL();
        /// <summary>
        /// 继电器使用锁
        /// </summary>
        public static readonly object RelayPortLock = new object();
        public static List<Model.StepModel.Step> ChannelsStep = new List<Model.StepModel.Step>();
        public static TextBlock textBlockCount = null;

        private bool WaitSacn = false;
        private BLL.TestViewBLL testViewBLL;
        /// <summary>
        /// 通道日志页面
        /// </summary>
        public ChannelLogView channelLogView = new ChannelLogView();
        /// <summary>
        /// 蓝牙日志页面
        /// </summary>
        public BtLogView btLogView;
        /// <summary>
        /// 继电器串口
        /// </summary>
        public SerialPort RelayPort = new SerialPort();
        /// <summary>
        /// 蓝牙模块串口
        /// </summary>
        public SerialPort BtPort = new SerialPort();
        /// <summary>
        /// 蓝牙搜索定时器
        /// </summary>
        public DispatcherTimer ScanTimer = new DispatcherTimer();
        /// <summary>
        /// 通道1
        /// </summary>
        public SingelTestView singelTestView1 = new SingelTestView();
        /// <summary>
        /// 通道2
        /// </summary>
        public SingelTestView singelTestView2 = new SingelTestView();
        /// <summary>
        /// 通道3
        /// </summary>
        public SingelTestView singelTestView3 = new SingelTestView();
        /// <summary>
        /// 通道4
        /// </summary>
        public SingelTestView singelTestView4 = new SingelTestView();
        /// <summary>
        /// 通道5
        /// </summary>
        public SingelTestView singelTestView5 = new SingelTestView();

        public TestView()
        {
            try
            {
                HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-cn");
                InitializeComponent();

                textBlockCount = TextBlockCount;
                //初始化
                btLogView = new BtLogView(this);
                testViewBLL = new BLL.TestViewBLL(this);

                //读取配置文件
                config = testViewBLL.GetConfigModel();
                //初始化串口
                testViewBLL.InitPort(config);
                //初始化通道
                testViewBLL.InitChannel(config);
                //打开蓝牙，继电器串口
                testViewBLL.OpenPorts();
                //定时器搜索蓝牙
                ScanTimer.Tick += new EventHandler(ScanTimer_Tick);
                ScanTimer.Interval = new TimeSpan(10000000);
                ScanTimer.Start();
                isScan = true;
                

                //计数
                int count = resultDAL.GetTodayCount();
                TextBlockCount.Text = count.ToString();
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void ButtonSet_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                isScan = false;
                //重新读取配置文件
                config = testViewBLL.GetConfigModel();
                //设置页面，返回结果
                bool? configChange = new View.SetView().ShowDialog();
                if (configChange == true)
                {
                    //发生更改，取消进程初始化界面
                    singelTestView1.BorderCancel_MouseLeftButtonUp(null, null);
                    singelTestView2.BorderCancel_MouseLeftButtonUp(null, null);
                    singelTestView3.BorderCancel_MouseLeftButtonUp(null, null);
                    singelTestView4.BorderCancel_MouseLeftButtonUp(null, null);
                    singelTestView5.BorderCancel_MouseLeftButtonUp(null, null);
                    //初始化串口
                    testViewBLL.InitPort(config);
                    //初始化通道
                    testViewBLL.InitChannel(config);
                    //打开蓝牙 继电器串口
                    testViewBLL.OpenPorts();
                }
                //isScan = true;
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }

        }

        private void ButtonChannelLog_Click(object sender, RoutedEventArgs e)
        {
            //显示通道日志
            channelLogView.WindowState = WindowState.Normal;
            channelLogView.Focus();
            channelLogView.Show();
        }

        private void ButtonStartAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //五个通道全部开始测试
                isScan = true;
                singelTestView1.BorderChannel_MouseLeftButtonUp(null, null);
                singelTestView2.BorderChannel_MouseLeftButtonUp(null, null);
                singelTestView3.BorderChannel_MouseLeftButtonUp(null, null);
                singelTestView4.BorderChannel_MouseLeftButtonUp(null, null);
                singelTestView5.BorderChannel_MouseLeftButtonUp(null, null);
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void ButtonBtLog_Click(object sender, RoutedEventArgs e)
        {
            //蓝牙模块搜索页面
            btLogView.WindowState = WindowState.Normal;
            btLogView.Focus();
            btLogView.Show();
        }
        private void ScanTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                //判断并开始搜索
                if (isScan == true && WaitSacn == false)
                {
                    Scan();
                }

            }
            catch (Exception ex)
            {
                WaitSacn = false;
                isScan = false;
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }
        /// <summary>
        /// 搜索蓝牙
        /// </summary>
        public void Scan()
        {
            try
            {
                //读取配置串口
                if (config.BtPortname == "")
                {
                    UiBLL.RichTextBoxAppendText(btLogView.RichTextBoxBt, Brushes.Tomato, "未配置蓝牙串口");
                    isScan = false;
                    return;
                }
                if (BtPort.IsOpen == false)
                {
                    BtPort.Open();
                }
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        List<BtLabelModel.DeviceModel> macs = null;
                        WaitSacn = true;
                        UiBLL.RichTextBoxClearText(btLogView.RichTextBoxBt);
                        //获取蓝牙
                        macs = testViewBLL.GetMacs();
                        //显示
                        lock (MacsLock)
                        {
                            Macs = macs;
                            btLogView.InitMacs();
                        }
                        WaitSacn = false;
                    }
                    catch (Exception ex)
                    {
                        WaitSacn = false;
                        isScan = false;
                        Growl.Error(ex.Message);
                        BtLabelTool.LogHelper.WriteLog(ex.ToString());
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ButtonPowerOn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (RelayPort.IsOpen==false)
                {
                    RelayPort.Open();
                }
                BtLabelTool.PortHelper.SendData(RelayPort, BtLabelModel.PortModel.RelayUartModel.AllOn);
                BtLabelTool.PortHelper.GetData(RelayPort, 1000, 100);
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }
        private BtLabelBLL.MultimeterBLL multimeterBLL = new MultimeterBLL();
        private void ButtonCurrent_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                multimeterBLL._addr = config.MultimeterUsbAddr;
                if (config.MultimeterUsbAddr=="")
                {
                    Growl.Warning("请先配置万用表地址");
                    return;
                }
                double cur = 0;
                multimeterBLL.GetCurrent(ref cur);
                Growl.Info(cur.ToString());
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }

        }
    }
}
